package com.shizhanzhe.zxbapp.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.shizhanzhe.zxbapp.R;
import com.shizhanzhe.zxbapp.bean.NEW;
import com.shizhanzhe.zxbapp.bean.UserBean;
import com.shizhanzhe.zxbapp.customtask.CreditCardDetailsActivity;
import com.shizhanzhe.zxbapp.customtask.CreditTaskDetailsActivity;
import com.shizhanzhe.zxbapp.customtask.ScreenTaskDetailsActivity;
import com.shizhanzhe.zxbapp.home.InvitationActivity;
import com.shizhanzhe.zxbapp.pay.PayVIP;
import com.shizhanzhe.zxbapp.task.FollowActivity;
import com.shizhanzhe.zxbapp.task.WebActivity;
import com.shizhanzhe.zxbapp.task.newshare.ShareWebActivity;
import com.shizhanzhe.zxbapp.url.Path;
import com.shizhanzhe.zxbapp.utils.OkHttpDownloadJsonUtil;
import com.shizhanzhe.zxbapp.vip.GameActivity;
import com.shizhanzhe.zxbapp.widget.MyListView;
import com.shizhanzhe.zxbapp.widget.VipDIalog;

import java.util.List;

/**
 * Created by zz9527 on 2018/12/13.
 */
public class NewActivity extends Activity {
    private MyListView lv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new);
        ImmersionBar.with(this).fitsSystemWindows(true).statusBarColor(R.color.colorAccent).init();
        findViewById(R.id.backarea).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        final SmartRefreshLayout refresh = findViewById(R.id.refresh);
        refresh.setRefreshHeader(new MaterialHeader(this));
        lv = findViewById(R.id.new_lv);
        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refresh.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                getData();
            }
        });
        getData();
    }

    private void getData() {
        OkHttpDownloadJsonUtil.downloadJson(this, Path.NEW(UserBean.uid), new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {
            @Override
            public void onsendJson(String json) {
                if (!"1".equals(json) && !"2".equals(json)) {
                    try {


                        Gson gson = new Gson();
                        final List<NEW.DataBean> data = gson.fromJson(json, NEW.class).getData();
                        lv.setAdapter(new NewAdapter(NewActivity.this, data));
                        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                NEW.DataBean dataBean = data.get(i);
                                final Intent intent = new Intent();
                                if (dataBean.getFclass() != 6) {
                                    switch (dataBean.getFclass()) {
                                        case 1://分享
//                                            intent.setClass(NewActivity.this, WebActivity.class);
//                                            intent.putExtra("url", dataBean.getUrl());
//                                            intent.putExtra("shid", dataBean.getId());
//                                            intent.putExtra("fen", dataBean.getFen());
//                                            intent.putExtra("title", dataBean.getTitle());
//                                            intent.putExtra("img", dataBean.getLogo());
//                                            intent.putExtra("type", 0);
//                                            intent.putExtra("intro", dataBean.getShcon());
//                                            intent.putExtra("isshow", dataBean.getIsshow());
//                                            intent.putExtra("scon", dataBean.getShowtext());
//                                            intent.putExtra("ym", dataBean.getYuming());
                                            if (UserBean.level > 0) {
                                                intent.setClass(NewActivity.this, ShareWebActivity.class).putExtra("id", dataBean.getId());
                                            } else {
                                                if(dataBean.getIsvip()==1){
                                                    final VipDIalog vipDIalog = new VipDIalog(NewActivity.this);
                                                    vipDIalog.setVIPListener(new VipDIalog.VIPListener() {
                                                        @Override
                                                        public void chooseTrue() {
                                                            startActivity(new Intent(NewActivity.this, PayVIP.class));
                                                            vipDIalog.dismiss();
                                                        }

                                                        @Override
                                                        public void chooseFalse() {
                                                            vipDIalog.dismiss();
                                                        }
                                                    });
                                                    vipDIalog.show();
                                                    return;
                                                }else {
                                                    intent.setClass(NewActivity.this, ShareWebActivity.class).putExtra("id", dataBean.getId());
                                                }

                                            }
                                            break;
                                        case 2://关注
                                            intent.setClass(NewActivity.this, FollowActivity.class);
                                            intent.putExtra("iv", dataBean.getLogo());
                                            intent.putExtra("title", dataBean.getTitle());
                                            intent.putExtra("one", dataBean.getDan());
                                            intent.putExtra("all", dataBean.getMoney());
                                            intent.putExtra("wechatnum", dataBean.getWxunid());
                                            intent.putExtra("num", dataBean.getNum() + "");
                                            intent.putExtra("sum", dataBean.getSum() + "");
                                            if (dataBean.getSex().contains("男")) {
                                                intent.putExtra("sex", 1);
                                            } else if (dataBean.getSex().contains("女")) {
                                                intent.putExtra("sex", 2);
                                            }
                                            break;
                                        case 3://信用卡
                                            intent.setClass(NewActivity.this, CreditCardDetailsActivity.class);
                                            intent.putExtra("taskid", dataBean.getId());
                                            break;
                                        case 4://截图任务
                                            intent.setClass(NewActivity.this, CreditTaskDetailsActivity.class);
                                            intent.putExtra("taskid", dataBean.getId());
                                            break;
                                        case 5:
                                            intent.setClass(NewActivity.this, InvitationActivity.class);
                                            break;
//                            case 6:
//                                setResult(1);
//                                finish();
//                                break;
                                        case 7:
                                            intent.setClass(NewActivity.this, GameActivity.class);
                                            break;
                                        default:
                                            break;
                                    }
                                    startActivity(intent);
                                } else {
                                    setResult(1);
                                    finish();
                                }
                            }
                        });
                    } catch (Exception e) {
                        Toast.makeText(NewActivity.this, "数据异常", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(NewActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }

        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
